package com.jhd.fuzhou.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class ChartView extends View {
    public int XPoint;    //原点的X坐标
    public int YPoint;     //原点的Y坐标
    public int XScale;     //X的刻度长度
    public int YScale;     //Y的刻度长度
    public int XLength;//获取屏幕分辨率Constant.point.x-120;        //X轴的长度
    public int YLength;        //Y轴的长度
    public String[] XLabel;    //X的刻度
    public String[] YLabel;    //Y的刻度
    public String[] Data;      //数据
    public String Title;    //显示的标题
    private String mUnit;
    private Context mContext;
    public ChartView(Context context){
        super(context);
    }
    public ChartView(Context context, @Nullable AttributeSet attrs){
        super(context,attrs);
    }
    public ChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public void SetInfo(Context context,int width,String unit,String[] XLabels,String[] YLabels,String[] AllData,String strTitle){
        XLength=width-DipAndDp.dip2px(context,50);
        YLength=DipAndDp.dip2px(context,300);
        YPoint=DipAndDp.dip2px(context,280);
        XPoint=DipAndDp.dip2px(context,30);
        XScale=(width-100)/XLabels.length;
        YScale=DipAndDp.dip2px(context,260)/YLabels.length;
        XLabel=XLabels;
        YLabel=YLabels;
        Data=AllData;
        Title=strTitle;
        mContext=context;
        mUnit=unit;
        /*XScale=XLength/AllData.length;//实际X的刻度长度
        YScale=YLength/YLabels.length;*/
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);//重写onDraw方法

        //canvas.drawColor(Color.WHITE);//设置背景颜色
        Paint paint= new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//去锯齿
        paint.setColor(Color.BLACK);//颜色
        Paint paint1=new Paint();
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(Color.DKGRAY);
        paint.setTextSize(36);  //设置轴文字大小
        //设置Y轴
        canvas.drawLine(XPoint, DipAndDp.dip2px(mContext,20), XPoint, YPoint, paint);   //轴线
        for(int i=0;i<YLabel.length ;i++){
            canvas.drawLine(XPoint,YPoint-i*YScale, XPoint+5, YPoint-i*YScale, paint);  //刻度
            try{
                canvas.drawText(YLabel[i] , 0, YPoint-i*YScale+5, paint);  //文字
            }catch(Exception e){
            }
        }
        canvas.drawLine(XPoint,DipAndDp.dip2px(mContext,20),XPoint-DipAndDp.dip2px(mContext,3),DipAndDp.dip2px(mContext,26),paint);  //箭头
        canvas.drawLine(XPoint,DipAndDp.dip2px(mContext,20),XPoint+DipAndDp.dip2px(mContext,3),DipAndDp.dip2px(mContext,26),paint);
        //设置字体的大小角度等
        paint.setTextSize(36);
        drawText(canvas,mUnit, XPoint+20, DipAndDp.dip2px(mContext,20), paint,0);

        //设置X轴
        paint.setTextSize(36);
        canvas.drawLine(XPoint,YPoint,XPoint+XLength,YPoint,paint);   //轴线
        for(int i=0;i<XLabel.length;i++){
            canvas.drawLine(XPoint+i*XScale, YPoint, XPoint+i*XScale, YPoint-5, paint);  //刻度
            try{
                canvas.drawText(XLabel[i], XPoint + i * XScale - 10,DipAndDp.dip2px(mContext,300), paint); // 文字
               /*drawText(canvas,XLabel[i], XPoint + i * XScale,
                        YPoint + 40, paint,-45); // 文字*/
                // 数据值
                if (i > 0 && YCoord(Data[i - 1]) != -999
                        && YCoord(Data[i]) != -999) // 保证有效数据
                    canvas.drawLine(XPoint + (i - 1) * XScale,
                            YCoord(Data[i - 1]), XPoint + i * XScale,
                            YCoord(Data[i]), paint);
                canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2,
                        paint);
            } catch (Exception e) {
            }
        }
        canvas.drawLine(XPoint+XLength,YPoint,XPoint+XLength-DipAndDp.dip2px(mContext,6),YPoint-DipAndDp.dip2px(mContext,3),paint);    //箭头
        canvas.drawLine(XPoint+XLength,YPoint,XPoint+XLength-DipAndDp.dip2px(mContext,6),YPoint+DipAndDp.dip2px(mContext,3),paint);
        //设置标题位置
        paint.setTextSize(45);
        canvas.drawText(Title, XLength/2-28, 50, paint);
    }
    //设置文字显示方向
    void drawText(Canvas canvas ,String text , float x ,float y,Paint paint ,float angle){
        if(angle != 0){
            canvas.rotate(angle, x, y);
        }
        canvas.drawText(text, x, y, paint);
        if(angle != 0){
            canvas.rotate(-angle, x, y);
        }
    }

    private int YCoord(String y0)  //计算绘制时的Y坐标，无数据时返回-999
    {
        int y;
        try
        {
            y=Integer.parseInt(y0);
        }
        catch(Exception e)
        {
            return -999;    //出错则返回-999
        }
        try
        {
            return YPoint-y*YScale/Integer.parseInt(YLabel[1]);
        }
        catch(Exception e)
        {
        }
        return y;
    }

}
